<!-- Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -->
<!DOCTYPE html>
<title>Unit Test of e2e.BigPrimeNum</title>
<script src="../../../../../javascript/closure/base.js"></script>
<script src="test_js_deps-runfiles.js"></script>
<script>
  goog.require('goog.testing.jsunit');
  goog.require('e2e.BigPrimeNum');
</script>
<script>
/**
* Does an elgamal decryption.
*/
function testElgamal() {
var x = [0x03,0x60,0xd2,0x08,0xe0,0x1f,0x16,0x99,0xbd,0xdd,0xb9,0xe9,
         0x59,0x28,0xf3,0x60,0x61,0xd8,0x1f,0xb4,0xa0,0xfe,0x0f,0x72,
         0xd2,0xff,0x66,0xd5,0x5b,0xa3,0x84,0x6c];

var p = [0x9f,0x4a,0xcf,0xac,0x8b,0x65,0x05,0xb5,0x15,0xba,0x82,0xab,
         0xf8,0x8c,0xa2,0xa1,0x01,0xf8,0x0a,0xfd,0x19,0x82,0x37,0x39,
         0x03,0xaa,0xf2,0xac,0x33,0x8a,0x76,0xa9,0x13,0x41,0xea,0x1f,
         0x95,0x68,0xcd,0x5b,0x1a,0xa1,0x92,0xc7,0xe6,0x01,0xb6,0xef,
         0x76,0x0d,0xb7,0xf9,0x5e,0xbe,0xe5,0x6b,0xba,0xa4,0x12,0x83,
         0x6a,0xc1,0x21,0x67,0x45,0x70,0x3c,0xb4,0xe7,0x47,0xe2,0xd6,
         0x59,0x58,0x1b,0x73,0x23,0x12,0x63,0x13,0xdf,0x11,0x87,0xc0,
         0x7b,0x55,0x7b,0x63,0xef,0x60,0x3f,0x69,0x50,0x87,0x78,0xd9,
         0x8d,0x17,0x64,0xbb,0x6b,0x33,0x0a,0xb8,0xc6,0xcc,0xed,0xf9,
         0xb7,0x02,0x9e,0x4d,0x0f,0x7f,0x45,0x6b,0x65,0xea,0xe4,0x0e,
         0xef,0xe3,0xbe,0x1b,0xbf,0xbc,0xf9,0x53];

// v=m*y**k;
var v=[0x2f,0xe4,0xda,0xff,0xd7,0xa7,0x6a,0xc4,0x15,0x26,0x1e,0x48,
       0xa3,0xd9,0xda,0xd3,0x93,0x94,0x75,0x3b,0x9d,0x47,0x66,0xd8,
       0x1e,0xde,0xe3,0x6e,0xe9,0x24,0xd7,0x1a,0xf8,0xd9,0xf5,0xd1,
       0x29,0xba,0x3e,0x85,0xb6,0x61,0x79,0x0c,0xeb,0xb1,0x9c,0xb4,
       0x6c,0x63,0xcd,0x55,0xed,0x6e,0xb1,0xb9,0x25,0x45,0xad,0x4c,
       0xeb,0x3e,0x67,0x93,0x54,0xaf,0x64,0x95,0xb8,0xb0,0x0d,0xc4,
       0xe3,0xac,0x22,0x9e,0xe9,0xc9,0x99,0x05,0x5d,0xa6,0x78,0x0a,
       0xa6,0xd9,0x72,0x84,0x4a,0xb5,0xc2,0x30,0x1d,0x5a,0x61,0x32,
       0xe1,0x07,0xaf,0x01,0x5d,0x3f,0x03,0xde,0xe8,0xfc,0xce,0x13,
       0x71,0xb8,0x6b,0x34,0xae,0xd0,0xfd,0xbb,0xe8,0xfb,0xe3,0xed,
       0x5a,0x24,0xd5,0x81,0xd2,0xc9,0xb5,0x75];

// u=g**k;
var u=[0x31,0x16,0x44,0xe9,0xcc,0x66,0x3c,0x5c,0x9a,0x4f,0x3a,0x75,
       0x56,0xc3,0x65,0xc5,0x8e,0x00,0xff,0xa7,0x82,0x98,0x2f,0x9d,
       0xd2,0x51,0x27,0xd1,0x01,0x27,0xc7,0x1b,0x74,0x8a,0xad,0x46,
       0x0f,0xbb,0x9e,0x5c,0x3b,0x28,0xac,0x58,0x19,0x59,0xaa,0xa0,
       0xeb,0x65,0x6d,0xf3,0x69,0x24,0xc3,0xde,0xf6,0xc5,0xfb,0x69,
       0x9b,0x1b,0xf7,0x5d,0xf6,0x47,0x5a,0xde,0xd1,0x74,0x19,0x01,
       0x29,0x9a,0x68,0xb4,0xb7,0x83,0x3a,0x0a,0x6c,0x50,0x2e,0xf3,
       0x8a,0x3d,0x5d,0x0d,0x15,0x1b,0x3e,0x8a,0x16,0x83,0xc0,0xc6,
       0x34,0x9d,0xd7,0xf5,0x2c,0x4b,0x6f,0xb7,0x41,0x5f,0x4b,0x33,
       0x12,0x20,0x58,0x65,0x65,0x97,0xc3,0xbc,0x04,0xd9,0xba,0xd8,
       0x9e,0x98,0xa5,0xaa,0x86,0x0a,0xe7,0x3e];

var P = new e2e.BigPrimeNum(p);
var result = P.mul(v, P.pow(u, P.negateExponent(x)));
assertArrayEquals(
  [0x02,0x99,0x67,0xe1,0x0f,0xb7,0x41,0xa7,0xf1,0xcb,0xac,0x06,0xb8,
   0xf2,0x5d,0x52,0x23,0x6d,0xc3,0xb6,0x5e,0xca,0x9c,0xc4,0xf4,0x58,
   0xb1,0x4d,0x84,0xff,0x74,0x03,0x2a,0x65,0x42,0xb3,0xf5,0x22,0xac,
   0x2c,0xfc,0xbe,0x98,0xc1,0xb7,0x2d,0x0d,0xef,0xa6,0x82,0x81,0x7d,
   0x58,0xc2,0x86,0x44,0x0a,0xa9,0x4a,0x3c,0x07,0x19,0xbd,0xb7,0x54,
   0x05,0x45,0x59,0x19,0x54,0x92,0xb6,0x73,0x83,0x76,0xa1,0x3f,0x0e,
   0xaa,0x67,0x29,0x66,0x04,0x62,0x8b,0xb7,0x18,0xb2,0x08,0x6b,0x65,
   0x00,0x09,0x91,0x3f,0x51,0xbe,0x28,0xce,0x46,0xfe,0xb0,0x1a,0x03,
   0xcc,0x78,0x86,0xe8,0x9c,0xdc,0x04,0x68,0x7c,0xe1,0xc3,0x1c,0xdf,
   0xf2,0x3f,0xd0,0x29,0x05,0xc2,0xe1,0xa3,0x11,0x0c],
  result);
}

/**
 * Tests Diffie Hellman key exchange.
 */
function testDiffieHellman() {
  var p = [0xFF,0xFF,0xFF,0xFE,0x00,0x00,0x00,0x00,0x75,0xA3,0x0D,0x1B,
           0x90,0x38,0xA1,0x15];
  var g = [0x02];
  var p_ = new e2e.BigPrimeNum(p);
  var a = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77];
  var A = p_.pow(g, a);
  var b = [0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33];
  var B = p_.pow(g, b);
  var s1 = p_.pow(B, a);
  var s2 = p_.pow(A, b);
  assertArrayEquals(s1, s2);
}

</script>
